package com.xinzhitong.www.utils;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class StuListExcel {
    public static String stulist(List<Map<String, Object>> x) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hhmmss");

        Workbook wb = new XSSFWorkbook();

        String[] title = {"date", "contact", "userId", "realname", "username"};
        String sheetName = "培训报名情况表格";
        Sheet stuSheet = wb.createSheet(sheetName);
        Row titleRow = stuSheet.createRow(0);
        CellStyle style = wb.createCellStyle();
        Cell cell = null;

        for (int i = 0; i < title.length; i++) {
            cell = titleRow.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        Row row = null;
        for (int i = 0; i < x.size(); i++) {
            row = stuSheet.createRow(i + 1);

            String a = x.get(i).get("date").toString();
            row.createCell(0).setCellValue(a);
            if (x.get(i).get("contact") != null) {
                row.createCell(1).setCellValue(x.get(i).get("contact").toString());
            }
            if (x.get(i).get("userId") != null) {
                row.createCell(2).setCellValue(x.get(i).get("userId").toString());
            }

            if (x.get(i).get("realname") != null) {
                row.createCell(3).setCellValue(x.get(i).get("realname").toString());
            }
            a = x.get(i).get("username").toString();
            row.createCell(4).setCellValue(a);

        }

        for (int i = 0; i < title.length; i++) {
            stuSheet.autoSizeColumn(i, true);
            stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10);
        }

        String path = "/static/excel";
        String folderPath = "D:/excel/stuInfoExcel";
        File folder = new File(folderPath);
        //如果文件夹不存在创建对应的文件夹
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String fileName = sdf1.format(new Date()) + sheetName + ".xls";
        String savePath = folderPath + File.separator + fileName;

//        OutputStream fileOut = null;
//        try {
//            fileOut = new FileOutputStream(savePath);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        try {
//            wb.write(fileOut);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        try {
//            fileOut.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            wb.write(bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] barray = bos.toByteArray();

        //is第一转
        InputStream is = new ByteArrayInputStream(barray);
        //需要导包
        //如果是spring boot把下面的导入pom.xml
        try {
            MultipartFile multipartFile = new MockMultipartFile(fileName, fileName, "", is);
            FileHelper.saveTo(path, multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回文件保存全路径
        return FileHelper.fileURL + path;
    }
}
